// Copyright 2013 Square, Inc.

package com.hemaapp.yjnh.view.calendar;

import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.view.calendar.MonthCellDescriptor.RangeState;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

/**个人注解：CalendarCellView是日历中的一天
 * @author GuiyouZhang
 * 2015年9月26日  下午11:17:42
 */
public class CalendarCellView extends TextView implements View.OnClickListener {
    
private static final int[] STATE_SELECTABLE = {
    R.attr.state_selectable
};
private static final int[] STATE_CURRENT_MONTH = {
    R.attr.state_current_month
};
private static final int[] STATE_TODAY = {
    R.attr.state_today
};
private static final int[] STATE_HIGHLIGHTED = {
    R.attr.state_highlighted
};
private static final int[] STATE_RANGE_FIRST = {
    R.attr.state_range_first
};
private static final int[] STATE_RANGE_MIDDLE = {
    R.attr.state_range_middle
};
private static final int[] STATE_RANGE_LAST = {
    R.attr.state_range_last
};

private boolean isSelectable = false;
private boolean isCurrentMonth = false;
private boolean isToday = false;
private boolean isHighlighted = false;
private RangeState rangeState = RangeState.NONE;

private MonthView.Listener2 listener2;
private  CalendarCellView view;

public CalendarCellView(Context context, AttributeSet attrs) {
  super(context, attrs);
  
}


public void setSelectable(boolean isSelectable) {
  this.isSelectable = isSelectable;
  refreshDrawableState();
}

public void setCurrentMonth(boolean isCurrentMonth) {
  this.isCurrentMonth = isCurrentMonth;
  refreshDrawableState();
}

public void setToday(boolean isToday) {
  this.isToday = isToday;
  refreshDrawableState();
}

public void setRangeState(MonthCellDescriptor.RangeState rangeState) {
  this.rangeState = rangeState;
  refreshDrawableState();
}

public void setHighlighted(boolean highlighted) {
  isHighlighted = highlighted;
  refreshDrawableState();
}
@Override
public void onClick(View v) {
    // Header rows don't have a click listener
    if (listener2 != null) {
        listener2.handleClick(view,(MonthCellDescriptor) v.getTag());
      }
  }
public void setListener2(MonthView.Listener2 listener2,CalendarCellView view) {
    this.listener2 = listener2;
    this.view = view;
  }
@Override protected int[] onCreateDrawableState(int extraSpace) {
  final int[] drawableState = super.onCreateDrawableState(extraSpace + 5);
  //给“每天”在原有的正常、按下、可用、不可用等属性的基础上再添加5个属性，如下：
  if (isSelectable) {
    mergeDrawableStates(drawableState, STATE_SELECTABLE);
  }

  if (isCurrentMonth) {
    mergeDrawableStates(drawableState, STATE_CURRENT_MONTH);
  }

  if (isToday) {
    mergeDrawableStates(drawableState, STATE_TODAY);
  }

  if (isHighlighted) {
    mergeDrawableStates(drawableState, STATE_HIGHLIGHTED);
  }

  if (rangeState == MonthCellDescriptor.RangeState.FIRST) {
    mergeDrawableStates(drawableState, STATE_RANGE_FIRST);
  } else if (rangeState == MonthCellDescriptor.RangeState.MIDDLE) {
    mergeDrawableStates(drawableState, STATE_RANGE_MIDDLE);
  } else if (rangeState == RangeState.LAST) {
    mergeDrawableStates(drawableState, STATE_RANGE_LAST);
  }

  return drawableState;
}
}
